package com.systemframework.sys.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.systemframework.annotation.LicenseAnnotation;
import com.systemframework.annotation.OperateAnnotation;
import com.systemframework.common.BaseCommonInterface;
import com.systemframework.enums.BusinessType;
import com.systemframework.sys.dto.request.SysRoleRequestDTO;
import com.systemframework.sys.dto.response.SysRoleResponseDTO;
import com.systemframework.sys.entity.SysRole;
import com.systemframework.sys.service.SysRoleService;
import com.systemframework.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author summer
 * @date 2023-03-20 13:34
 */
@RestController
@RequestMapping(value = "/role")
@Slf4j
public class SysRoleController implements BaseCommonInterface<SysRoleRequestDTO,SysRoleResponseDTO> {


    private SysRoleService sysRoleService;

    @Autowired
    public void setSysRoleService(SysRoleService sysRoleService) {
        this.sysRoleService = sysRoleService;
    }

    @LicenseAnnotation
    @OperateAnnotation(businessType = BusinessType.INSERT,name = "角色信息")
    @Override
    public Result<String> save(SysRoleRequestDTO sysRoleRequestDTO) {
        SysRole sysRole = Convert.convert(SysRole.class, sysRoleRequestDTO);
        sysRoleService.save(sysRole);
        return Result.successResult();
    }

    @LicenseAnnotation
    @OperateAnnotation(businessType = BusinessType.UPDATE,name = "角色信息")
    @Override
    public Result<String> update(SysRoleRequestDTO sysRoleRequestDTO) {
        SysRole sysRole = Convert.convert(SysRole.class, sysRoleRequestDTO);
        sysRoleService.updateById(sysRole);
        return Result.successResult();
    }

    @LicenseAnnotation
    @OperateAnnotation(businessType = BusinessType.DELETE,name = "角色信息")
    @Override
    public Result<String> del(Integer id) {
        sysRoleService.removeById(id);
        return Result.successResult();
    }

    @LicenseAnnotation
    @Override
    public Result<Page<SysRoleResponseDTO>> lists(SysRoleRequestDTO sysRoleRequestDTO) {
        return Result.successResult(sysRoleService.findLists(sysRoleRequestDTO));
    }

    @LicenseAnnotation
    @Override
    public Result<SysRoleResponseDTO> findById(Integer id) {
        SysRole sysRole = sysRoleService.getById(id);
        return Result.successResult(Convert.convert(SysRoleResponseDTO.class,sysRole));
    }

    @LicenseAnnotation
    @OperateAnnotation(businessType = BusinessType.BATCHDELETE,name = "角色信息")
    @Override
    public Result<String> batchDel(List<SysRoleResponseDTO> lists) {
        List<Integer> ids = lists.stream().map(SysRoleResponseDTO::getId).collect(Collectors.toList());
        if(ids.size()>0 && CollUtil.isNotEmpty(ids)){
            sysRoleService.removeByIds(ids);
        }
        return Result.successResult();
    }


    @LicenseAnnotation
    @PostMapping(value = "/saveRoleAndMenus")
    public Result<String> saveRoleAndMenus(@RequestBody SysRoleRequestDTO sysRoleRequestDTO){
        sysRoleService.saveRoleAndMenus(sysRoleRequestDTO);
        return Result.successResult();
    }

    private SysRoleService roleService;

    @Autowired
    public void setRoleService(SysRoleService roleService) {
        this.roleService = roleService;
    }

    @LicenseAnnotation
    @GetMapping(value = "/roleLists")
    public Result<List<SysRoleResponseDTO>> roleLists(){
        List<SysRoleResponseDTO> sysRoleResponseDTOList = Convert.convert(new TypeReference<List<SysRoleResponseDTO>>() {}, roleService.list());
        return Result.successResult(sysRoleResponseDTOList);
    }

}
